﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; 
using System.Web.Mvc;
using CollegeShop.WebProcessor;
using CollegeShop.Infrastructure.Entities.Enum;
using CollegeShop.Biz;
using CollegeShop.Infrastructure.Entities.Entity;
using CollegeShop.Tool.Framework;

namespace CollegeShop.Web.Controllers
{
    [ErrorHandle]
    [Auth(SystemRole.Admin)]
    public class AdminController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        #region 楼栋
        public ActionResult Dormitory()
        {
            ViewBag.Dormitory = BaseInfoBiz.GetDormitories(true);
            return View();
        }
        public ActionResult AddDormitory()
        {
            ViewBag.Dormitory = BaseInfoBiz.GetDormitories();
            return View();
        }
        [HttpPost]
        public ActionResult AddDormitory(string DormitoryName)
        {
            DormitoryName = DormitoryName.Trim();
            if(!new Dormitory().Any(c=>c.DormitoryName == DormitoryName))
            {
                new Dormitory(){
                    DormitoryName = DormitoryName,
                    SalesUnitID=0
                }.PostAdd();
            }
            return RedirectToAction("AddDormitory");
        }
        public int editDormitory(string method,string text, int id)
        {
            if (method == "edit")
            {
                if (!String.IsNullOrEmpty(text))
                {
                    var dormitory = new Dormitory().Find(id.ToString());
                    if (dormitory != null)
                    {
                        dormitory.DormitoryName = text;
                        dormitory.PostModify();
                    }
                }
            }
            else if(method=="delete")
            { 
                var dormitory = new Dormitory().Find(id.ToString());
                if (dormitory != null)
                {
                    dormitory.PostDelete();
                }
            }
            return 1;
        }
        #endregion

        #region 销售单元
        public ActionResult Unit()
        {
            ViewBag.Unit = BaseInfoBiz.GetSalesUnit(true);
            ViewBag.Dormitory = BaseInfoBiz.GetFreeDormitories();
            return View();
        }
        /// <summary>
        /// 获取销售单元信息
        /// </summary>
        /// <returns></returns>
        public string getunitinfo(int id)
        {
            var unit = new Salesunit().Find(id.ToString());
            if (unit != null) {
                return new
                {
                    id = id,
                    name = unit.SalesUnitName,
                    manager = unit.LinkMan,
                    phone = unit.LinkPhone
                }.ToJson();
            }
            return "{}";
        }
        public ActionResult AddUnit()
        {
            ViewBag.Staff = UserBiz.GetStaffs();
            ViewBag.Unit = BaseInfoBiz.GetSalesUnit(true);
            ViewBag.Dormitories = BaseInfoBiz.GetDormitories();
            return View();
        }
        [HttpPost]
        public ActionResult AddUnit(string SalesUnitName, string LinkMan, string LinkPhone)
        {
            if (new User().Any(c=>c.UserName == LinkMan) &&
                !new Salesunit().Any(c=>c.SalesUnitName == SalesUnitName))
            {
                new Salesunit()
                {
                    LinkMan = LinkMan,
                    LinkPhone = LinkPhone,
                    SalesUnitName = SalesUnitName,
                    SalesUnitNamePY = ""
                }.PostAdd();
            }
            return RedirectToAction("AddUnit");
        }
        #endregion

        #region 用户管理
        public ActionResult Group()
        {
            ViewBag.Staffs = UserBiz.GetStaffGroup();
            return View();
        }
        public ActionResult NewManager()
        {
            ViewBag.Staffs = UserBiz.GetStaffGroup();
            return View();
        }
        [HttpPost]
        public ActionResult NewManager(string userName, string password, string NickName, string Phone, int SystemRole)
        {
            if (!new User().Any(c => c.UserName == userName))
            {
                new User()
                {
                    UserName = userName,
                    Password = CollegeShop.Tool.Framework.AesAlgorithm.Encrypt(password),
                    IsActivate = true,
                    IsRefuse = false,
                    RealName = NickName,
                    NickName = NickName,
                    Phone = Phone,
                    SystemRole = (SystemRole)SystemRole,
                    CreateDate = DateTime.Now,
                    Email = string.Empty
                }.PostAdd();
            }
            return RedirectToAction("NewManager");
        }
        public int deleteuser(string username)
        {
            var user = new User().Find(username);
            if (user != null) {
                user.PostDelete();
                return 1;
            }
            return 0;
        }
        #endregion
    }
}
